package com.neu.af.controller;

import com.neu.af.common.base.BaseModel;
import com.neu.af.common.base.BaseModelJson;
import com.neu.af.dataFilter.entity.User;
import com.neu.af.dataFilter.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/userOp")
public class LoginController {
    @Autowired
    private LoginServiceImpl loginService;

    //登录接口
    @PostMapping("/login")
    public BaseModelJson<Map> login(@RequestParam(value = "name") String name,
                                    @RequestParam(value = "password") String password) {
        BaseModelJson<Map> result = new BaseModelJson<>();
        Map<String, Object> map = new HashMap<>();
//        List<User> users = loginService.selectByNamePassword(name,password);
        User user = loginService.getLoginInfo(name, password);
        map.put("user", user);
        result.code = 200;
        result.message = "success";
        result.data = map;
        return result;
    }

    @PostMapping("/add")
    public BaseModel add(@RequestParam(value = "name") String name,
                         @RequestParam(value = "password") String password,
                         @RequestParam(value = "type") Integer type,
                         @RequestParam(value = "typeName") String typeName){
        int status = loginService.insert(new User(name, password, type, typeName));
        BaseModel result = new BaseModel();
        result.code = 200;
        result.message = status==-1?"用户名已存在":"success";
        return result;
    }

    @PostMapping("/delete")
    public BaseModel delete(Integer id){
        loginService.deleteByPrimaryKey(id);
        BaseModel result = new BaseModel();
        result.code = 200;
        result.message = "success";
        return result;
    }

    @PostMapping("/update")
    public BaseModel update(@RequestParam(value = "id") Integer id,
                            @RequestParam(value = "name") String name,
                            @RequestParam(value = "password") String password,
                            @RequestParam(value = "type") Integer type,
                            @RequestParam(value = "typeName") String typeName){
        int status = loginService.updateByPrimaryKey(new User(id, name, password, type, typeName));
        BaseModel result = new BaseModel();
        result.code = 200;
        result.message = status==-1?"用户名已存在":"success";
        return result;
    }

    @PostMapping("/select")
    public BaseModelJson<Map> select(@RequestParam(value = "name", defaultValue = "") String name,
                                     @RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestParam(value = "page",defaultValue="1") Integer page) {
        BaseModelJson<Map> result = new BaseModelJson<>();
        Map<String, Object> map = loginService.selectByName(name,page, pageSize);
        map.put("currentPage", page);
        map.put("pageSize", pageSize);
        result.code = 200;
        result.message = "success";
        result.data = map;
        return result;
    }
}
